﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

/*
 *游戏进程控制器
 * 玩家状态控制
 * 地图信息控制
 */
public class GameControl : MonoBehaviour
{
    public int GameStageNum = 0;
    [SerializeField]private int GemNum=0, EneNum=0;
    public static int MaxHp = 3;
    public int curGem=0;
    private PlayerControl pl;
    private Map m;
    [SerializeField]private UIControl pl_UI;
    public Camera MainCm;

    public static bool is_pause;
    // Start is called before the first frame update
    private void Awake()
    {
        is_pause = false;
        pl_UI = GameObject.Find("PlayerPlane").GetComponent<UIControl>();
    }

    void Start()
    {
        m = GetComponent<Map>();
        MainCm = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public IEnumerator GameStart()//初始化游戏地图,玩家状态
    {
        m.SetMap(GameStageNum);
        m.CreateMap();
        pl_UI.gameObject.SetActive(true);
        pl_UI.StartGame();
        pl_UI.SetGem(GemNum);
        pl_UI.SetHealth(MaxHp);
        GemNum = m.NumOfGem;
        EneNum = m.NumOfEne;
       
        yield return new WaitForSeconds(0.1f);
        is_pause = true;
        pl = GameObject.FindWithTag("Player").GetComponent<PlayerControl>();
        pl.PlayerStart(MaxHp);
        MainCm.gameObject.AddComponent<FollowCamera>();
    }

    public void GetGem(int a)
    {
        curGem+=a;
        pl_UI.SetGem(curGem);
        if (curGem>=GemNum)
        {
            StartCoroutine(pl_UI.GameNext());
            Destroy(MainCm.GetComponent<FollowCamera>());
            NextStage();
            StartCoroutine(DestroyStage());
        }
    }

    public void PlayerUnderAttack()
    {
        pl.DecreaseHp();
        pl_UI.SetHealth(pl.GetHp());
        if (pl.GetHp() <= 0)
        {
            StartCoroutine(pl_UI.GameOver());
            Destroy(MainCm.GetComponent<FollowCamera>());
            Destroy(pl.GetComponent<GridMove>());
            StartCoroutine(Restart());
            StartCoroutine(DestroyStage());
        }
    }

    public void MonsterDead()
    {
        EneNum--;
        if (EneNum<=0)
        {
            StartCoroutine(pl_UI.GameNext());
            pl.GetComponent<BoxCollider>().enabled = false;
            NextStage();
            StartCoroutine(DestroyStage());
        }
    }
    private void NextStage()
    {
        GameStageNum ++;
        StartCoroutine(Restart());
    }

    private IEnumerator Restart()
    {
        yield return new WaitForSeconds(0.71f);
        GemNum = 0;
        EneNum = 0;
        curGem = 0;
        is_pause = false;
    }

    private IEnumerator DestroyStage()
    {
        yield return new WaitUntil(()=>StartButton.Candestroy);
        yield return new WaitForSeconds(1f);
        //摧毁实例
        GameObject g = GameObject.Find("Ground");
        for (int i = 0; i < g.transform.childCount; i++)
        {
            Transform t = g.transform.GetChild(i);
            for (int j = 0; j < t.childCount; j++)
            {
                Destroy(t.transform.GetChild(j).gameObject);
            }
        }
        Destroy(MainCm.GetComponent<FollowCamera>());
    }
    public void GameOver()
    {
        HitStop(true);
        print("gameover");
    }
    private void HitStop(bool check)
    {
        is_pause = check;
    }
    public void OnAttack()
    {
        MainCm.transform.GetComponent<FollowCamera>().OnAttack();
        HitStop(true);
    }

    public void EndAttack()
    {
        MainCm.transform.GetComponent<FollowCamera>().EndAttack();
        HitStop(false);
    }

    public bool IsPause()
    {
        return is_pause;
    }
}
